﻿using System;
using System.Collections.Generic;

namespace eLoop;

public static class Scheduler
{
    public static bool IsStandardVersion
    {
        get 
        {
#if NETSTANDARD2_0 || NETSTANDARD2_1
            return true;
#elif NETCOREAPP
            return false;
#endif
        }
    }
    
    private static Dictionary<bool, Func<ITaskScheduler>> queueSchedulerBuilderMap = new Dictionary<bool, Func<ITaskScheduler>>()
    {
        {true, () => { return new SingleSyncQueueScheduler();}},
        {false, () => { return new SingleUnsyncQueueScheduler();}},
    };
    private static Dictionary<bool, Func<ITaskScheduler>> threadBuilderMap = new Dictionary<bool, Func<ITaskScheduler>>()
    {
        {true, () => { return new SingleSyncThreadScheduler();}},
        {false, () => { return new SingleUnsyncThreadScheduler();}},
    };
    
    public static ITaskScheduler Inline { get; } = new InlineScheduler();

    public static ITaskScheduler ThreadPool { get; } = new ThreadPoolScheduler();
    
    public static bool Equals(ITaskScheduler l, ITaskScheduler r)
    {
        if (l == null || r == null)
        {
            return false;
        }

        return l.Id == r.Id;
    }

    public static ITaskScheduler CreateEventScheduler()
    {
        return new SingleEventScheduler();
    }

    public static ITaskScheduler CreateThreadScheduler(bool syncContext)
    {
        return threadBuilderMap[syncContext]();
    }

    public static ITaskScheduler CreateQueueScheduler(bool syncContext)
    {
        return queueSchedulerBuilderMap[syncContext]();
    }

    public static void AddThreadRunner(Action func)
    {
        ThreadRunner.AddRunner(func);
    }
}